﻿namespace Sidvall.WorkItemManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class MonitorExecutionContext : global::Sidvall.WorkItemManager.UI.Entities.Core.BusinessBase, global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext
    {
        #region Business Members

        #region MonitorExecutionContextId

        private System.Nullable<int> _MonitorExecutionContextId = null;
        partial void OnBeforeMonitorExecutionContextIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData);
        partial void OnBeforeMonitorExecutionContextIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterMonitorExecutionContextIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<int> MonitorExecutionContextId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<int>>(_MonitorExecutionContextId);
                OnBeforeMonitorExecutionContextIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _MonitorExecutionContextId);
                OnBeforeMonitorExecutionContextIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("MonitorExecutionContextId", eventData.Value, true, true, true);
                    OnAfterMonitorExecutionContextIdPropertyChanged();
                }
            }
        }

        #endregion
        #region WorkItemAlarms

        private WorkItemAlarmCollection _WorkItemAlarms;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public WorkItemAlarmCollection WorkItemAlarms
        {
            get
            {
                return _WorkItemAlarms;
            }
            internal set
            {
                LoadInstanceField("WorkItemAlarms", value, true, false, false);
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowIsValid

        public override bool DataRowIsValid(Sidvall.Data.BusinessRuleFilter filter)
        {
            return global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.DataRowIsValid(this, filter);
        }

        #endregion
        #region DataRowStateIsDirty

        public override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.DataRowStateIsDirty(this);
            }
        }

        #endregion

        #region LoadWorkItemAlarms

        public void LoadWorkItemAlarms(WorkItemAlarmCollection value)
        {
            LoadInstanceField("WorkItemAlarms", value, true, true, false);
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public MonitorExecutionContext Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public MonitorExecutionContext Copy()
        {
            return Copy(null);
        }
        public MonitorExecutionContext Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new MonitorExecutionContext();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(MonitorExecutionContext destination, global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(MonitorExecutionContext destination, global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(MonitorExecutionContext destination, MonitorExecutionContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(MonitorExecutionContext destination, MonitorExecutionContext source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.WorkItemManager.UI.SystemContextConfig.Current.EntityFacade.MonitorExecutionContextManager.Initialize(this, settings);
        }

        #endregion
        
        #region IMonitorExecutionContext Members

        System.Nullable<int> global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext.MonitorExecutionContextId
        {
            get
            {
                return this._MonitorExecutionContextId;
            }
            set
            {
                this._MonitorExecutionContextId = value;
            }
        }
        System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWorkItemAlarm> global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext.WorkItemAlarms
        {
            get
            {
                return this._WorkItemAlarms;
            }
        }
        void global::Sidvall.WorkItemManager.Data.IMonitorExecutionContext.SetWorkItemAlarms(Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWorkItemAlarm> items)
        {
            UpdateChildProperty(this._WorkItemAlarms, items);
            this._WorkItemAlarms = items as WorkItemAlarmCollection;
        }

        #endregion

        #endregion
        #region Constructors

        public MonitorExecutionContext()
        {
        }

        public MonitorExecutionContext(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}